﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace GameControler
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        //[STAThread]




        //public static IntPtr GameIntPtr;
        static MainWindow instance;
        public static MainWindow Instance
        {
            get { return instance; }
        }

        public MainWindow()
        {
            instance = this;
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;
        }

        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                //GameIntPtr = NativeHelper.GetHandleList((int)(new WindowInteropHelper(this).Handle), GameHelper.GameName);//.FindWindow(null, GameHelper.GameName);
                //if (!GameIntPtr.Equals(IntPtr.Zero))
                //{
                //    this.Title = "已经连接到虚拟机";
                //}
                //else
                //{
                //    this.Title = "无法连接到虚拟机";

                //}
            }
            catch { }
            //Mdain();
        }

        protected override void OnPreviewKeyDown(KeyEventArgs e)
        {
            switch (e.Key)
            {
                case Key.A:
                    {
                        GameShow.GameHandle.SendClick(GameHelper.ReturnButton);
                        break;
                    }


                case Key.S:
                    {
                        GameShow.GameHandle.SendClick(GameHelper.ReturnHomeButton);
                        break;
                    }

                case Key.D:
                    {
                        GameShow.GameHandle.SendClick(GameHelper.GiveButton);
                        break;
                    }

                case Key.F:
                    {
                        GameShow.GameHandle.SendClick(GameHelper.SetButton);
                        break;
                    }

                case Key.W:
                    {
                        GameShow.GameHandle.SendClick(GameHelper.StartButton);
                        break;
                    }

                case Key.Q:
                    {
                        GameShow.GameHandle.SendClick(GameHelper.EnterButton);
                        break;
                    }

                case Key.O:
                    {
                        GameShow.GameHandle.SendClick(GameHelper.OpenButton);
                        break;
                    }
                case Key.Enter:
                    {
                        int x = int.Parse(this.X.Text);
                        int y = int.Parse(this.Y.Text);

                        GameShow.GameHandle.SendClick(new System.Drawing.Point(x, y));
                        break;
                    }

                case Key.RightShift:
                    {
                        if (isdown)
                        {
                            break;
                        }
                        isdown = true;
                        int x = int.Parse(this.X.Text);
                        int y = int.Parse(this.Y.Text);
                        GameShow.GameHandle.SendMove(new System.Drawing.Point(x, y));
                        GameShow.GameHandle.SendDown(new System.Drawing.Point(x, y));
                        break;
                    }
            }

        }

        bool isdown = false;

        protected override void OnPreviewKeyUp(KeyEventArgs e)
        {
            base.OnPreviewKeyUp(e);
            switch (e.Key)
            {
                case Key.RightShift:
                    {
                        isdown = false;
                        int x = int.Parse(this.X.Text);
                        int y = int.Parse(this.Y.Text);

                        GameShow.GameHandle.SendUp(new System.Drawing.Point(x, y));
                        break;
                    }
            }
        }


        //protected override void OnPreviewKeyDown(KeyEventArgs e)
        //{
        //    base.OnPreviewKeyDown(e);
        //    switch (e.Key)
        //    {
        //        case Key.A:
        //            {
        //                GameIntPtr.SendDown(GameHelper.LeftButton);
        //                break;
        //            }
        //        case Key.D:
        //            {
        //                GameIntPtr.SendDown(GameHelper.RightButton);
        //                break;
        //            }
        //        case Key.W:
        //            {
        //                GameIntPtr.SendDown(GameHelper.TopButton);
        //                break;
        //            }
        //        case Key.S:
        //            {
        //                GameIntPtr.SendDown(GameHelper.BottomButton);
        //                break;
        //            }

        //        case Key.Z:
        //            {
        //                GameIntPtr.SendClick(GameHelper.HitAuto);
        //                break;
        //            }

        //        case Key.X:
        //            {
        //                GameIntPtr.SendClick(GameHelper.HitSelf);
        //                break;
        //            }

        //        case Key.Q:
        //            {
        //                GameIntPtr.SendClick(GameHelper.HitEnter);
        //                break;
        //            }

        //        case Key.NumPad0:
        //            {
        //                GameIntPtr.SendClick(GameHelper.HitDefaultButton);
        //                break;
        //            }
        //        case Key.NumPad1:
        //            {
        //                GameIntPtr.SendClick(GameHelper.Hit1Button);
        //                break;
        //            }
        //        case Key.NumPad2:
        //            {
        //                GameIntPtr.SendClick(GameHelper.Hit2Button);
        //                break;
        //            }
        //        case Key.NumPad3:
        //            {
        //                GameIntPtr.SendClick(GameHelper.Hit3Button);
        //                break;
        //            }
        //        case Key.NumPad4:
        //            {
        //                GameIntPtr.SendClick(GameHelper.Hit4Button);
        //                break;
        //            }

        //        case Key.NumPad6:
        //            {
        //                GameIntPtr.SendClick(GameHelper.HitOtherButton);
        //                break;
        //            }

        //        case Key.Enter:
        //            {
        //                GameIntPtr.SendClick(GameHelper.ReturnHomeButton);
        //                break;
        //            }
        //    }

        //}

        //protected override void OnPreviewKeyUp(KeyEventArgs e)
        //{
        //    base.OnPreviewKeyUp(e);

        //    switch (e.Key)
        //    {
        //        case Key.A:
        //            {
        //                GameIntPtr.SendUp(GameHelper.LeftButton);
        //                break;
        //            }
        //        case Key.D:
        //            {
        //                GameIntPtr.SendUp(GameHelper.RightButton);
        //                break;
        //            }
        //        case Key.W:
        //            {
        //                GameIntPtr.SendUp(GameHelper.TopButton);
        //                break;
        //            }
        //        case Key.S:
        //            {
        //                GameIntPtr.SendUp(GameHelper.BottomButton);
        //                break;
        //            }
        //    }

        //}

        //private void Button_Click(object sender, RoutedEventArgs e)
        //{
        //    //var isovernow = GameIntPtr.JudgeView();
        //    //return;
        //    startbtn.IsEnabled = false;

        //    //System.Timers.Timer timer = new System.Timers.Timer(1000);
        //    //timerClose = timer;
        //    //timer.Elapsed += timer_Elapsed;
        //    //timer.Start();


        //    canauto = true;
        //    StartGame();
        //}

        //bool canauto = true;

        //Thread t;
        //public void StartGame()
        //{
        //    t = new Thread((ParameterizedThreadStart)delegate
        //        {
        //            GameIntPtr.SendMove(GameHelper.BottomButton, 1000);
        //            Thread.Sleep(2000);
        //            GameIntPtr.SendMove(GameHelper.TopButton, 2000);
        //            Thread.Sleep(4000);

        //            GameIntPtr.SendClick(GameHelper.HitEnter);
        //            Thread.Sleep(10000);

        //            GameIntPtr.SendClick(GameHelper.HitAuto);

        //            System.Timers.Timer timer = new System.Timers.Timer(1500);
        //            timerClose = timer;
        //            timer.Elapsed += timer_Elapsed;
        //            timer.Start();
        //        });
        //    t.Start();
        //}

        //System.Timers.Timer timerClose;

        //bool isover = false;
        //void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        //{
        //    if (t != null)
        //    {
        //        t.Abort();
        //    }

        //    var isovernow = GameIntPtr.JudgeView();
        //    if (isovernow)
        //    {
        //        GameIntPtr.SendClick(GameHelper.ReturnHomeButton);
        //        isover = true;
        //    }

        //    if (isover == true && isovernow == false)
        //    {
        //        isover = false;

        //        System.Timers.Timer timer = sender as System.Timers.Timer;
        //        timer.Elapsed -= timer_Elapsed;
        //        timer.Stop();
        //        timer.Close();
        //        GameIntPtr.SendClick(GameHelper.ReturnHomeButton);
        //        Thread.Sleep(1000);
        //        GameIntPtr.SendClick(GameHelper.ReturnHomeButton);

        //        Thread.Sleep(8000);
        //        if (canauto)
        //        {
        //            StartGame();
        //        }
        //    }
        //}

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            //if (timerClose != null)
            //{
            //    timerClose.Close();
            //}

            //startbtn.IsEnabled = true;
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            //canauto = false;
            //GameIntPtr.SendClick(GameHelper.HitAuto);

            //System.Timers.Timer timer = new System.Timers.Timer(1500);
            //timerClose = timer;
            //timer.Elapsed += timer_Elapsed;
            //timer.Start();
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            GameShow.Instance.Start();//.IsPause = true;
        }

        private void Button_Click_4(object sender, RoutedEventArgs e)
        {

            GameShow.Instance.End();//.IsPause = false;
        }


        //canauto = false;

        //System.Timers.Timer timer = new System.Timers.Timer(50);
        //timerClose = timer;
        //timer.Elapsed += timer_Elapsed1;
        //timer.Start();

        //int hitcount = 0;
        //bool isleft = false;
        //void timer_Elapsed1(object sender, System.Timers.ElapsedEventArgs e)
        //{

        //    hitcount++;

        //    if (hitcount < 30)
        //    {
        //        return;
        //    }

        //    GameIntPtr.SendClick(GameHelper.HitDefaultButton);

        //    if (hitcount >= 300)
        //    {
        //        if (isleft)
        //        {
        //            GameIntPtr.SendMove(GameHelper.RightButton, 3000);
        //        }
        //        else
        //        {
        //            GameIntPtr.SendMove(GameHelper.LeftButton, 3000);
        //        }
        //        isleft = !isleft;

        //        hitcount = 0;

        //        return;
        //    }

        //    if (hitcount == 50 || hitcount == 100 || hitcount == 150 || hitcount == 200 || hitcount == 250)
        //    {
        //        GameIntPtr.SendClick(GameHelper.Hit1Button);
        //        GameIntPtr.SendClick(GameHelper.Hit2Button);
        //        GameIntPtr.SendClick(GameHelper.Hit3Button);
        //        GameIntPtr.SendClick(GameHelper.Hit4Button);
        //    }

        //}


        protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
        {
            base.OnPreviewMouseDown(e);
            //Mouse.Capture(this);

            //this.Cursor = Cursors.Cross;
            //iscan = true;
        }

        bool iscan = false;

        protected override void OnPreviewMouseMove(MouseEventArgs e)
        {
            base.OnPreviewMouseMove(e);

            if (iscan)
            {
                //Mdain();
            }

        }

        protected override void OnPreviewMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            base.OnPreviewMouseLeftButtonUp(e);



            //this.Cursor = Cursors.Arrow;
            //iscan = false;
        }


        private void Button_Click(object sender, RoutedEventArgs e)
        {

        }




        private void Start_Click(object sender, RoutedEventArgs e)
        {
            GameShow.Instance.Start();//.IsPause = true;

            this.startbtn.IsEnabled = false;
        }

        private void Stop_Click(object sender, RoutedEventArgs e)
        {
            GameShow.Instance.End();//.IsPause = false;
            this.startbtn.IsEnabled = true;
        }

        private void Close_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown(0);
        }


    }
}
